package Function;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Vector;

import VWorldGameStudio.FCItem;
import VWorldGameStudio.FCPlot;
import utils.FCDataSingles;

public class FCPlotLibrary {
	public Vector<String> plotId = new Vector<>();
	private Vector<FCPlot> plotList = new Vector<>();
	private Vector<String> plotTouchOff = new Vector<>();
	private static final String PLOT_LIST_PATH = "./data/plotlist.vw";
	private static final String PLOTS_PATH = "./data/plot";
	private int plotallnum=0;
	/**
	 * 初始化剧情类，读取数量
	 * 建议游戏启动方使用
	 * @date 2021-11-30
	 */
	public FCPlotLibrary() {
		try {
			this.plotallnum=new File(PLOTS_PATH).listFiles().length;
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 剧情数据加载函数
	 * @date 2021-11-30
	 */
	public void loadPlotData() { 
		try {
			File[] f=new File(PLOTS_PATH).listFiles();
			for(int i=0;i<plotallnum;i++) {
				FCPlot plot = FCDataSingles.getReader().readPlotData(f[i].getPath());
				plotList.add(plot);
				plotId.add(plot.getId());
				plotTouchOff.add(plot.getTouchoff());
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取剧情实际数据量
	 * @return integer
	 * @date 2021-11-30
	 */
	public int getPlotAllNum() {
		return this.plotallnum;
	}
	/**
	 * 初始化剧情
	 * 
	 * @param isGame
	 */
	public FCPlotLibrary(boolean isGame) {
		try {
			File[] f=new File(PLOTS_PATH).listFiles();
			this.plotallnum = f.length;
			for(int i=0;i<plotallnum;i++) {
				FCPlot plot = FCDataSingles.getReader().readPlotData(f[i].getPath());
				plotList.add(plot);
				plotId.add(plot.getId());
				plotTouchOff.add(plot.getTouchoff());
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void registerPlot(boolean isnew, String id, String touchoff, Vector<String> active) {
		FCPlot pl = new FCPlot();
		pl.registerPlot(id, touchoff, active);
		if (isnew) {
			plotId.add(id);
			plotTouchOff.add(touchoff);
			plotList.add(pl);
		} else {
			plotList.remove(plotId.indexOf(id));
			plotTouchOff.remove(plotId.indexOf(id));
			plotId.remove(id);
			plotList.add(pl);
			plotTouchOff.add(touchoff);
			plotId.add(id);
		}
		FCDataSingles.getWriter().writePlotData(pl);
	}

	public String getActiveOfIdAndNum(String id, int num) {
		return plotList.get(plotId.indexOf(id)).getActiveOfNum(num);
	}

	public int getPlotNum() {
		return plotList.size();
	}

	public String getIdOfNum(int i) {
		return plotId.get(i);
	}

	public FCPlot getPlotOfId(String id) {
		return plotList.get(plotId.indexOf(id));
	}

	public Vector<String> getPlotID() {
		return this.plotId;
	}

	public Vector<String> getPlotTouoff() {
		return this.plotTouchOff;
	}
}
